package com.jinbai.servlet.album;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jinbai.service.AlbumService;
import com.jinbai.util.Log;
import com.jinbai.view_object.UserVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/studio/album/album.json")
public class BindJsonServlet extends HttpServlet {
    private final AlbumService albumService = new AlbumService();
    private final ObjectMapper om = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UserVO userVO = null;
        req.setCharacterEncoding("utf-8");
        final HttpSession session = req.getSession(false);
        if (session != null) {
            userVO = (UserVO) session.getAttribute("currentUser");
        }
        if (userVO == null) {
            Log.println("用户未登录");
            resp.sendRedirect("/studio/user/login.html");
            return;
        }
        final int aid = Integer.parseInt(req.getParameter("aid"));

        Object result = albumService.getBindInfo(userVO, aid);
        final String s = om.writeValueAsString(result);
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        final PrintWriter writer = resp.getWriter();
        writer.println(s);
    }
}
